var express = require('express');
var app = express();
var config = require('./config');
var Mock = require('mockjs');
var data = require('./data');

console.log('Now listening at Port:%d',config.port);
app.listen(config.port);

app.all('*',function(req,res,next){
	if(!req.get('Origin')) return next();
	res.set('Access-Control-Allow-Origin','*');
	res.set('Access-Control-Allow-Methods','GET,POST');
	res.set('Access-Control-Allow-Headers','X-Requested-With, Content-Type');

	if(res.Method === 'OPTIONS') return res.send('200');
	next();
})

Object.keys(config.getUrl).forEach(key=>{
	app.get(config.getUrl[key],function(req,res,next){
		res.json(Mock.mock(data[key]));
	})
});

Object.keys(config.postUrl).forEach(key=>{
	app.post(config.postUrl[key],function(req,res,next){
		res.json(Mock.mock(data[key]));
	})
});
